﻿using DotnetSpider.Core;
using DotnetSpider.Core.Processor;
using HtmlAgilityPack;
using Spider_Demo.Models;
using System;
using System.Collections.Generic;
using System.Text;

namespace Spider_Demo.PageProcesser
{
    public class MM131_Processor : BasePageProcessor
    {
        public string ImgType { get; private set; }
        public string ImgUrl { get; private set; }

        protected override void Handle(Page page)
        {
            var list = new List<MM_Images>();
            var modelHtmlList = page.Selectable.XPath("//div[@class='main']/dl/dd/a[@target]").Nodes();
            foreach (var modelHtml in modelHtmlList)
            {
                MM_Images entity = new MM_Images
                {
                    ImgTitle = modelHtml.XPath("./img/@alt").GetValue(),
                    ImgType = modelHtml.XPath("//div[@class='main']/dl/dt/a[2]").GetValue(),
                    Thumbnail_ImgUrl = modelHtml.XPath("./img/@src").GetValue(),
                    ImgInfo = GetInfo(modelHtml.XPath("./@href").GetValue())
                };
                //foreach (var item in entity.ImgInfo)
                //{
                //    page.AddTargetRequest(item.HD_ImgUrl); //Site设置DownloadFiles为TRUE就可以自动下载文
                //}
                list.Add(entity);
            }
            page.AddResultItem("MM_ImgList", list);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Url">详情页地址</param>
        /// <returns></returns>
        private List<ImgInfo> GetInfo(string Url)
        {
            //Url = Url.Replace("https","http");
            List<ImgInfo> imgInfos = new List<ImgInfo>();
            HtmlWeb htmlWeb = new HtmlWeb
            {
                OverrideEncoding = Encoding.GetEncoding("gb2312")
            };
            var PageEnd = int.Parse(htmlWeb.Load(Url).DocumentNode.SelectSingleNode("//div[@class='content-page']/span[1]").InnerText.Replace("共", "").Replace("页", ""));
            for (int i = 1; i <= PageEnd; i++)
            {
                HtmlWeb info = new HtmlWeb
                {
                    OverrideEncoding = Encoding.GetEncoding("gb2312")
                };
                string loadUrl;
                if (i != 1)
                {
                    loadUrl = Url.Replace(".html", $"_{i}.html");
                }
                else 
                {
                    loadUrl = Url;
                }
                var docHtml = info.Load(loadUrl);
                ImgInfo img = new ImgInfo
                {
                    ImgName = docHtml.DocumentNode.SelectSingleNode("//div[@class='content']/h5").InnerText
                };
                if (i == 1)
                {
                    img.ImgName += "(1)";
                }
                img.HD_ImgUrl = docHtml.DocumentNode.SelectSingleNode("//div[@class='content-pic']//img").Attributes["src"].Value;
                imgInfos.Add(img);
            }
            return imgInfos;
        }
    }
}
